package com.hgk.common.common.util;

import android.content.Context;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class FileUtil {
	public Object readFileOIS(File file) {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		Object obj = null;
		if (!file.exists())
			return null;
		else {
			try {
				fis = new FileInputStream(file);
				ois = new ObjectInputStream(fis);
				obj = ois.readObject();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					if (ois != null)
						ois.close();
					if (fis != null)
						fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			return obj;
		}
	}

	/**
	 * ObjectInputStream return Set/Map/Vector
	 * */
	public Object readFileOIS(String pathName) {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		Object obj = null;
		File file = new File(pathName);
		if (!file.exists())
			return null;
		else {
			try {
				fis = new FileInputStream(file);
				ois = new ObjectInputStream(fis);
				obj = ois.readObject();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					if (ois != null)
						ois.close();
					if (fis != null)
						fis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			return obj;
		}
	}

	/**
	 * ObjectOutputStream 直接写入vector map
	 * */
	public boolean writeFileOOS(String pathName, Object o, boolean append) {
		ObjectOutputStream oos = null;
		FileOutputStream fos = null;
		File file = new File(pathName);
		if (!file.exists())
			try {
				if (!file.createNewFile())
					return false;
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		if (o == null)
			return false;
		else {
			try {
				fos = new FileOutputStream(file, append);
				oos = new ObjectOutputStream(fos);
				oos.writeObject(o);
				oos.flush();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					if (oos != null)
						oos.close();
					if (fos != null)
						fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			return true;
		}
	}

	public boolean writeFileOOS(File file, Object o, boolean append) {
		ObjectOutputStream oos = null;
		FileOutputStream fos = null;
		if (!file.exists())
			try {
				if (!file.createNewFile())
					return false;
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		if (o == null)
			return false;
		else {
			try {
				fos = new FileOutputStream(file, append);
				oos = new ObjectOutputStream(fos);
				oos.writeObject(o);
				oos.flush();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					if (oos != null)
						oos.close();
					if (fos != null)
						fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			return true;
		}
	}

	/**
	 * create File
	 * */
	public boolean createFile(String pathName) {
		File file = new File(pathName);
		if (file.exists())
			return false;
		try {
			file.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;

	}
	/**
	 * 判断手机是否存在sd卡,并且有读写权限
	 *
	 * @return
	 */
	public static boolean isExistSdcard(Context context) {
		boolean flag = false;
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			flag = true;
		} else {

		}
		return flag;
	}
	/**
	 * 拷贝文件
	 *
	 * @param fromFile
	 * @param toFile
	 * @param rewrite
	 */
	public static void copyFile(File fromFile, File toFile, Boolean rewrite) {
		if (!fromFile.exists()) {
			return;
		}
		if (!fromFile.isFile()) {
			return;
		}
		if (!fromFile.canRead()) {
			return;
		}
		if (!toFile.getParentFile().exists()) {
			toFile.getParentFile().mkdirs();
		}
		if (toFile.exists() && rewrite) {
			toFile.delete();
		}

		try {
			FileInputStream fosfrom = new FileInputStream(fromFile);
			FileOutputStream fosto = new FileOutputStream(toFile);
			byte bt[] = new byte[1024];
			int c;
			while ((c = fosfrom.read(bt)) > 0) {
				fosto.write(bt, 0, c);
			}
			fosfrom.close();
			fosto.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
